[[ package gen;

import java.util.*;
import java.net.*;

import se.rupy.http.*;
import se.rupy.memory.*;
import se.rupy.sprout.*;
import se.rupy.content.*;
import se.rupy.util.*;

public class article extends Sprout {
	public String path() { return "/article"; }
	public void filter(Event event) throws Event, Exception {
		event.query().parse();
		event.reply().header("X-Pingback", "http://" + System.getProperty("host", "localhost") + "/ping");
		Output out = event.output();
		Object key = event.session().get("key");
		Article article = Article.find(event.big("id"));
		
		if(article == null) {
			out.println("Article [" + event.big("id") + "] not found!");
			throw event;
		}
		
		LinkedList users = article.child(USER);
		
		if(users.size() == 0) {
			event.reply().code("404 Not Found");
			event.reply().output().print("<pre>'" + event.query().path() + "' was not found.</pre>");
			throw event;
		}
		
		Node user = (Node) users.getFirst();
		boolean edit = user.meta(USER_KEY).equals(key);
		int permit = article.permit(key);
		article.poll(ARTICLE_READ); ]]

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>[[ article.meta(ARTICLE_TITLE) ]]</title>
  <link rel="stylesheet" type="text/css" href="res/sprout.css">
  
[[ String analytics = System.getProperty("analytics");
   if(analytics != null) { ]]

  <script type="text/javascript">
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', '[[ analytics ]]']);
    _gaq.push(['_trackPageview']);
    (function() {
      var ga = document.createElement('script');
      ga.src = 'http://www.google-analytics.com/ga.js';
      ga.setAttribute('async', 'true');
      document.documentElement.firstChild.appendChild(ga);
    })();
  </script>

[[ } ]]
  
  <script type="text/javascript" src="res/sprout.js"></script>
  <script type="text/javascript" src="res/cufon.js"></script>
  <script type="text/javascript" src="res/EasyScript.js"></script>
  <script type="text/javascript">
    Cufon.replace('h2');
  </script>
</head>
<body>
<div id="page">
  <div class="single">
    <table>
      <tr>
        <td colspan="2"><h2>[[ article.meta(ARTICLE_TITLE) ]]</h2></td>
      </tr>
      <tr>
        <td colspan="2"><i>[[ article.time() ]]</i></td>
      </tr>
      <tr>
        <td colspan="2">
        
[[			Iterator it = article.child(FILE).iterator();
			while(it.hasNext()) {
				Node file = (Node) it.next();
				Data name = file.meta(FILE_NAME);
				Data type = file.meta(FILE_TYPE);
				Data host = file.meta(FILE_HOST);
				
				if(type != null) {
					if(type.equals("IMAGE")) { ]]

          <div class="big">
            <img src="[[ File.path(event, file, name, "-400.jpeg") ]]" />
          </div>

[[ 					}
					else if(type.equals("VIDEO")) {
						String path = File.filepath(event, file, name, ".flv"); ]]

          <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="320" height="280" id="player" align="middle">
            <param name="allowScriptAccess" value="sameDomain"/>
            <param name="allowFullScreen" value="true"/>
            <param name="movie" value="video.swf?source=[[ path ]]"/>
            <param name="quality" value="high"/>
            <param name="bgcolor" value="#ffffff"/>
            <embed src="video.swf?source=[[ path ]]" quality="high" bgcolor="#ffffff" width="320" height="280" allowScriptAccess="sameDomain" allowFullScreen="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"/>
          </object>

[[					}
					else if(type.equals("AUDIO")) {
						String path = File.filepath(event, file, name, ".mp3"); ]]

          <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" height="30" width="200">
            <param name="movie" value="audio.swf?audio_file=[[ path ]]&color=E4E4E4"/>
            <embed src="audio.swf?audio_file=[[ path ]]&color=E4E4E4" height="30" width="200" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"/>
          </object>

[[					}
				}
			}

			LinkedList columns = article.columnize();
			it = columns.iterator();
			while(it.hasNext()) {
				String column = (String) it.next(); ]]

          <div class="[[ column.indexOf("<pre>") > -1 ? "table" : "column" ]]">[[ column ]]<span><img src="/res/continue.gif"></span></div>

[[			} ]]

        </td>
      </tr>
      <tr>
        <td colspan="2">/<a href="/[[ user.meta(USER_NAME) ]]">[[ user.meta(USER_NAME) ]]</a></td>
      </tr>
      
[[		LinkedList pings = article.child(PING); ]]

[[		if(article.pings > 0) { ]]

      <tr>
        <td colspan="2"><i>[[ article.pings ]]</i>&nbsp;[[ i18n("Pings") ]]</td>
      </tr>

[[		} ]]

[[		it = pings.iterator();
		while(it.hasNext()) {
			Node ping = (Node) it.next();
			Data title = ping.meta(PING_TITLE);
			Data url = ping.meta(PING_URL);
			Data state = ping.meta(PING_STATE);
			
			boolean show = (state == null ? false : state.equals("SHOW"));
      
[[			if(permit > 0) { ]]

      <tr>      
        <td><a href="[[ url ]]">[[ title ]]</a></td>
        <td>
          <a href="/[[ show ? "hide" : "show" ]]?id=[[ article.getId() ]]&ping=[[ ping.getId() ]]" class="button">
            <span>[[ i18n(show ? "Hide" : "Show") ]]</span>
          </a>
        </td>
      </tr>

[[			} else if(show) { ]]

      <tr>
        <td colspan="2"><a href="[[ url ]]">[[ title ]]</a></td>
      </tr>
      
[[			}
		} ]]

[[		LinkedList comments = article.child(COMMENT); ]]
      
      <tr>
        <td colspan="2"><img src="res/comment.gif" alt="[[ i18n("Comments") ]]"/></td>
      </tr>
      
[[		it = comments.iterator();
		while(it.hasNext()) {
			Node comment = (Node) it.next();
			Data ip = comment.meta(COMMENT_IP);
			Data body = comment.meta(COMMENT_BODY);
			Data state = comment.meta(COMMENT_STATE);
			
            boolean show = (state == null ? false : state.equals("SHOW"));
            
            if(ip == null) {
                try {
                    ip = ((Node) comment.child(USER).getFirst()).meta(USER_IP);
                }
                catch(NoSuchElementException e) {}
            } ]]
      
[[			if(permit > 0) { ]]

      <tr>
        <td><img src="res/quote.gif">&nbsp;[[ body ]]</td>
        <td>
          <a href="/[[ show ? "hide" : "show" ]]?id=[[ article.getId() ]]&comment=[[ comment.getId() ]]" class="button">
            <span>[[ i18n(show ? "Hide" : "Show") ]]</span>
          </a>
        </td>
        <td>

[[				try {
					String address = ip.getString();
					int index = address.indexOf(",");
					
					if(index > 0) {
						address = address.substring(index + 1).trim();
					} ]]
          [[ Ip.reverse(address) ]]
[[				} catch(Exception e) { ]]
          [[ e.getMessage() ]]
[[				} ]]

        </td>
      </tr>

[[			} else if(show) { ]]

      <tr>
        <td colspan="2"><img src="res/quote.gif">&nbsp;[[ body ]]</td>
      </tr>
      
[[			}
		} ]]
      
    </table>
  </div>
  <div class="comment">
    <table border="0" width="100">
      <form name="comment" action="comment" method="post">
        <input type="hidden" name="id" value="[[ article.getId() ]]"/>
        
[[		if(event.string("error").length() > 0) { ]]

        <tr>
          <td colspan="2">
            <font color="red"><i>[[ event.string("error") ]]</i></font>
          </td>
        </tr>

[[		} ]]

        <tr>
          <td colspan="2">
            <textarea id="body" class="large" name="body" rows="5" tabindex="2">[[ event.string("body") ]]</textarea>
          </td>
        </tr>
        <tr>
          <td colspan="2">
            <a href="" class="arrow" onclick="ajaxPost([[ article.getId() ]]); return false;">
              <span>[[ i18n("Post") ]]</span>
            </a>
          </td>
        </tr>
      </form>
    </table>
  </div>
</div>

<script type="text/javascript"> Cufon.now(); </script>

</body>
</html>

[[ } 
} ]]

